﻿using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Windows.Media.Imaging;
using System.IO;
using System.Xml.Serialization;
using Lib;


namespace SocketChat.control
{
    public partial class InkWrite : UserControl
    {
        Stroke _drawStroke;

        List<FillColor> lstFillColor;
        List<SizeData> lstSizeData;
        List<OpacityData> lstOpacityData;

        bool _isLoaded = false;

        public InkWrite()
        {
            InitializeComponent();

            //初始化数据
            lstFillColor = new List<FillColor>() { 
                new FillColor(){ Color = new SolidColorBrush(Colors.Black), Name="黑色"},
                new FillColor(){ Color = new SolidColorBrush(Colors.Red), Name="红色"},
                new FillColor(){ Color = new SolidColorBrush(Colors.Blue), Name="蓝色"},
                new FillColor(){ Color = new SolidColorBrush(Colors.Green),Name="绿色"},
                new FillColor(){ Color = new SolidColorBrush(Colors.Magenta), Name="洋红"},               
                new FillColor(){ Color = new SolidColorBrush(Colors.Orange), Name="橙色"},
            };

            lstSizeData = new List<SizeData>()
            {
                new SizeData(){ Size=1.0},
                new SizeData(){ Size=3.0},
                new SizeData(){ Size=5.0},
                new SizeData(){ Size=7.0},
                new SizeData(){ Size=9.0},
                new SizeData(){ Size=11.0},
                new SizeData(){ Size=13.0},
                new SizeData(){ Size=15.0}
               
            };

            lstOpacityData = new List<OpacityData>(){
                new OpacityData(){ Value=0.1},
                new OpacityData(){ Value=0.2},
                new OpacityData(){ Value=0.3},
                new OpacityData(){ Value=0.4},
                new OpacityData(){ Value=0.5},
                new OpacityData(){ Value=0.6},
                new OpacityData(){ Value=0.7},
                new OpacityData(){ Value=0.8},
                new OpacityData(){ Value=0.9},
                new OpacityData(){ Value=1.0}
            };

            this.Loaded += new RoutedEventHandler(InkWrite_Loaded);
        }

        void InkWrite_Loaded(object sender, RoutedEventArgs e)
        {
            this.cboColor.ItemsSource = lstFillColor;
            this.cboColor.SelectedIndex = 0;

            this.cboOutlineColor.ItemsSource = lstFillColor;
            this.cboOutlineColor.SelectedIndex = 0;

            this.cboWidth.ItemsSource = lstSizeData;
            this.cboWidth.SelectedIndex = 0;

            this.cboHeight.ItemsSource = lstSizeData;
            this.cboHeight.SelectedIndex = 0;

            this.cboOpactiy.ItemsSource = lstOpacityData;
            this.cboOpactiy.SelectedIndex = 9;

            _isLoaded = true;
        }

        private void OnMouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            ink.CaptureMouse();
            StylusPointCollection _spc = new StylusPointCollection();
            _spc.Add(e.StylusDevice.GetStylusPoints(ink));
            _drawStroke = new Stroke(_spc);
            _drawStroke.DrawingAttributes.Color = (cboColor.SelectedItem as FillColor).Color.Color;
            _drawStroke.DrawingAttributes.OutlineColor = (this.cboOutlineColor.SelectedItem as FillColor).Color.Color;
            _drawStroke.DrawingAttributes.Width = (this.cboWidth.SelectedItem as SizeData).Size;
            _drawStroke.DrawingAttributes.Height = (this.cboHeight.SelectedItem as SizeData).Size;            
            ink.Strokes.Add(_drawStroke);
            ink.Opacity = (cboOpactiy.SelectedItem as OpacityData).Value;
        }


        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (_drawStroke != null)
            {
                _drawStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(ink));
            }
        }

        private void cboOpactiy_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (_isLoaded)
            {
                ink.Opacity = (cboOpactiy.SelectedItem as OpacityData).Value;
            }
        }

        private void OnLostMouseCapture(object sender, MouseEventArgs e)
        {
            _drawStroke = null;
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            var opener = (this.LayoutRoot.Parent as InkWrite).Parent as ChildWindow;
            opener.Close();
            
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            ink.Strokes.Clear();
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            if (ink.Strokes.Count <= 0) 
            {
                MessageBox.Show("发送的内容不能为空!");
                return;
            }

            MainPage _root = (App.Current.RootVisual as MainPage);

            if (_root != null) 
            {
                WriteableBitmap wb = new WriteableBitmap(ink, null);
            
                _root.SendImage(wb);
                btnClose_Click(sender, e);
            }
        }
        
    }
}
